<template>
  <el-dialog :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="1000px" @open="open" @close="close">
    <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="100px" :disabled="disableEditSubmit">
      <el-row>
        <el-col :span="12">
          <el-form-item label="项目" prop="projectid" :rules="rules.validateRequire">
            <el-input v-model="model.dispprojectid" :disabled="true" class="tl-ref-selector">
              <el-button slot="append" type="primary" icon="el-icon-search" @click="openpmProjectRefDialog" v-if="!disableEditSubmit && isAdd"></el-button>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="项目角色" prop="role" :rules="rules.validateRequire">
            <el-select v-model="model.role" filterable clearable>
              <el-option v-for="item in codeModel.roleList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="姓名" prop="username" :required="true" :rules="rules.validateRequire">
            <tl-ref-user v-model="model.username" :initDisplayData.sync="model.username" dataType="code" :singleSelect="true" @confirm="onSelectUser" :disabled="disableEditSubmit || !isAdd"></tl-ref-user>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工号" prop="usercode">
            <el-input v-model="model.usercode" :readonly="true" class="is-disabled"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="联系电话" prop="phone" :rules="rules.validateRequire">
            <el-input v-model="model.phone"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="参与频度" prop="frequency" :rules="rules.validateRequire">
            <el-select v-model="model.frequency" filterable clearable>
              <el-option v-for="item in codeModel.frequencyList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="公司" prop="orgname">
            <el-input v-model="model.orgname" :readonly="true" class="is-disabled"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部门" prop="deptname">
            <el-input v-model="model.deptname" :readonly="true" class="is-disabled"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="上级领导" prop="leader">
            <tl-ref-user v-model="model.leader" :initDisplayData.sync="model.leader" dataType="code" :singleSelect="true" :disabled="disableEditSubmit" @confirm="onSelectLeader"></tl-ref-user>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="领导工号" prop="leadercode">
            <el-input v-model="model.leadercode" :readonly="true" class="is-disabled"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="职责" prop="duty">
            <el-input v-model="model.duty" type="textarea" :rows="2"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="description">
            <el-input v-model="model.description" type="textarea" :rows="2"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="有效" prop="enabled">
            <el-radio-group v-model="model.enabled">
              <el-radio :label="parseInt(item.code)" v-for="item in codeModel.enabledList" :key="item.code">{{ item.name }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit">取消</el-button>
      <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="disableEditSubmit === false">
        <svg-icon icon-class="save" /><span style="margin-left:2px">保存并关闭</span>
      </el-button>
      <!-- <el-button type="primary" @click="saveAndCopy" :loading="saveLoading" v-if="disableEditSubmit === false" icon="el-icon-copy-document">保存并复制</el-button> -->
      <el-popover placement="top-start" width="200" trigger="hover" content="项目和项目角色信息复制到当前页">
        <el-button slot="reference" type="primary" @click="saveAndCopy" :loading="saveLoading" v-if="disableEditSubmit === false" icon="el-icon-copy-document">保存并复制</el-button>
      </el-popover>
    </div>
    <tlpm-project-ref ref="pmProjectRef" @confirm="getpmProjectRefData" :isSingleSelect="true"></tlpm-project-ref>
  </el-dialog>
</template>

<script>
/** mixins */
import { mixin_edit } from "@tapui/mixins/mixin_edit"
/** 子组件 */
import tlpmProjectRef from "@/views/ProjectManage/pmProject/refList.vue"
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from "@tapui/utils/http"
/** VUE配置 */
export default {
  mixins: [mixin_edit],
  components: { tlpmProjectRef },
  data() {
    return {
      moduleTitle: "项目组成员",
      urlPrefix: "/pm/team/",
      url: {
        // 额外的url
        queryOrgDeptByIds: "/pm/team/queryOrgDeptByIds"
      }
    }
  },
  props: {
    projectId: {
      type: [Number, String],
      default: function() {
        return ""
      }
    },
    editFlag: {
      type: Boolean,
      default: true
    }
  },
  methods: {
    loaded() {
      // 页面加载完毕执行
    },
    onSelectUser(selectedUsers) {
      const selectedUser = selectedUsers[0]
      const { code, name, mobile, deptid, orgid } = selectedUser
      this.$set(this.model, "usercode", code)
      this.$set(this.model, "username", name)
      this.$set(this.model, "phone", mobile)
      const param = {
        deptId: deptid,
        orgId: orgid
      }
      getAction(this.url.queryOrgDeptByIds, param).then((res) => {
        if (res.success) {
          const { deptcode: deptcode, deptname: deptname, orgcode: orgcode, orgname: orgname } = res.result
          this.$set(this.model, "deptcode", deptcode)
          this.$set(this.model, "deptname", deptname)
          this.$set(this.model, "orgcode", orgcode)
          this.$set(this.model, "orgname", orgname)
        } else {
          this.$tap.alert("已选人员关联部门组织信息错误，请联系系统管理员")
        }
      })
    },
    onSelectLeader(selectedUsers) {
      const selectedUser = selectedUsers[0]
      const { code, name } = selectedUser
      this.$set(this.model, "leader", name)
      this.$set(this.model, "leadercode", code)
    },
    /** 打开项目参照 */
    openpmProjectRefDialog() {
      this.$refs.pmProjectRef.visible = true
    },
    /** 获取项目参数数据 */
    getpmProjectRefData(selectionRow, selectKey, selectionValue) {
      const { id, name } = selectionRow
      this.$set(this.model, "projectid", id)
      this.$set(this.model, "dispprojectid", name)
    },
    copyObj(obj) {
      const copyObj = Object.assign({}, obj)
      const { projectid, dispprojectid, role } = copyObj
      this.model = {}
      this.$set(this.model, "projectid", projectid)
      this.$set(this.model, "dispprojectid", dispprojectid)
      this.$set(this.model, "role", role)
    },
    saveAndCopy() {
      if (!this.beforeSubmit()) {
        return
      }
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.isAdd) {
            this.doAddNoCloseDialog()
          } else {
            this.doEditNoCloseDialog()
          }
        } else {
          return false
        }
      })
    },
    /** 执行新增提交 */
    doAddNoCloseDialog() {
      this.saveLoading = true
      postAction(this.url.add, this.model)
        .then((res) => {
          if (res.success) {
            this.$tap.message("添加成功", "success")
            this.$emit("refresh")
            this.copyObj(this.model)
          } else {
            this.$tap.alert(res.message, "warning")
          }
          this.afterSubmit()
          this.saveLoading = false
        })
        .catch((err) => {
          this.saveLoading = false
        })
    },
    /** 执行编辑提交 */
    doEditNoCloseDialog() {
      this.saveLoading = true
      putAction(this.url.edit, this.model)
        .then((res) => {
          if (res.success) {
            this.$tap.message(res.message, "success")
            this.$emit("refresh")
            this.isAdd = true
            this.copyObj(this.model)
          } else {
            this.$tap.alert(res.message, "warning")
          }
          this.afterSubmit()
          this.saveLoading = false
        })
        .catch((err) => {
          this.saveLoading = false
        })
    }
  }
}
</script>
